package _17_剑指Offer;

import _17_剑指Offer.datastrcute.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class _34_剑指Offer二叉树中和为某一值的路径 {

    private List<List<Integer>> results = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int target) {
        traverse(root, target, new ArrayList<>());
        return results;
    }


    public void traverse(TreeNode node, int target, List<Integer> list) {
        if (node == null) return;
        list.add(node.val);
        target -= node.val;
        if (node.left == null && node.right == null && target == 0)  results.add(new ArrayList<>(list));
        traverse(node.left, target, list);
        traverse(node.right, target, list);
        list.remove(list.size() - 1);
    }

}
